programming4us
           
 
 
Windows Phone

Programming Windows Phone 7 : Simple Clocks (part 2)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
1/6/2011 3:26:21 PM

An XNA clock program doesn’t need a timer because a timer is effectively built into the normal game loop. However, the clock I want to code here won’t display milliseconds so the display only needs to be updated every second. For that reason it uses the SuppressDraw method to inhibit superfluous Draw calls.

Here are the XnaSimpleClock fields:

Example 3. XNA Project: XnaSimpleClock File: Game1.cs (excerpt showing fields)
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont segoe14;
Viewport viewport;
Vector2 textPosition;
StringBuilder text = new StringBuilder();
DateTime lastDateTime;
. . .
}

Notice that instead of defining a field of type string named text, I’ve defined a StringBuilder instead. If you’re creating new strings in your Update method for display during Draw (as this program will do), you should use StringBuilder to avoid the heap allocations associated with the normal string type. This program will only be creating a new string every second, so I really didn’t need to use StringBuilder here, but it doesn’t hurt to get accustomed to it. StringBuilder requires a using directive for the System.Text namespace.

Notice also the lastDateTime field. This is used in the Update method to determine if the displayed time needs to be updated.

The LoadContent method gets the font and the viewport of the display:

Example 4. XNA Project: XnaSimpleClock File: Game1.cs (excerpt)
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
segoe14 = this.Content.Load<SpriteFont>("Segoe14");
viewport = this.GraphicsDevice.Viewport;
}

The logic to compare two DateTime values to see if the time has changed is just a little tricky because DateTime objects obtained during two consecutive Update calls will always be different because they have will have different Millisecond fields. For this reason, a new DateTime is calculated based on the current time obtained from DateTime.Now, but subtracting the milliseconds:

Example 5. XNA Project: XnaSimpleClock File: Game1.cs (excerpt)
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// Get DateTime with no milliseconds
DateTime dateTime = DateTime.Now;
dateTime = dateTime - new TimeSpan(0, 0, 0, 0, dateTime.Millisecond);

if (dateTime != lastDateTime)
{
text.Remove(0, text.Length);
text.Append(dateTime);
Vector2 textSize = segoe14.MeasureString(text);
textPosition = new Vector2((viewport.Width - textSize.X) / 2,
(viewport.Height - textSize.Y) / 2);
lastDateTime = dateTime;
}
else
{
SuppressDraw();
}

base.Update(gameTime);
}


At that point it’s easy. If the time has changed, new values of text, textSize, and textPosition are calculated. Because text is a StringBuilder rather than a string, the old contents are removed and the new contents are appended. The MeasureString method of SpriteFont has an overload for StringBuilder, so that call looks exactly the same.

If the time has not changed, SuppressDraw is called. The result: Draw is called only once per second.

DrawString also has an overload for StringBuilder:

Example 6. XNA Project: XnaSimpleClock File: Game1.cs (excerpt)
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Navy);

spriteBatch.Begin();
spriteBatch.DrawString(segoe14, text, textPosition, Color.White);
spriteBatch.End();

base.Draw(gameTime);
}

And here’s the result:



SuppressDraw can be a little difficult to use—I’ve found it particularly tricky during the time that the program is first starting up—but it’s one of the primary techniques used in XNA to reduce the power requirements of the program.

Other -----------------
- Windows Phone7: Pinning a Contact to Start
- Windows Phone7: Adding a Picture or Ringtone to a Contact
- Windows Phone7: Deleting a Contact
- Programming Windows Phone 7: XNA Orientation
- Programming Windows Phone 7: Orientation Events
- Windows Phone 7: Editing a Contact
- Windows Phone 7: Finding a Contact
- Windows Phone 7: Adding a Contact
- Windows Phone 7: Linking Contacts
- Programming Windows Phone 7 : Silverlight and Dynamic Layout (part 2)
- Programming Windows Phone 7 : Silverlight and Dynamic Layout (part 1)
- Programming Windows Phone 7 : An XNA Program for the Phone (part 3)
- Programming Windows Phone 7 : An XNA Program for the Phone (part 2)
- Programming Windows Phone 7 : An XNA Program for the Phone (part 1)
- Programming Windows Phone 7 : Points and Pixels
- Windows Phone 7 : Changing Caller ID Settings
- Windows Phone 7 : Forwarding Calls
- Windows Phone 7 : Checking Voicemail
- Windows Phone 7 : Making Conference Calls
- Programming Windows Phone 7 : Color Themes
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us